package cz.proks.easyhelpdesk.gae.core.db;

import java.util.List;

import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.beans.factory.annotation.Qualifier;
import org.springframework.stereotype.Component;

import cz.proks.easyhelpdesk.api.ThreadMemberApi;
import cz.proks.easyhelpdesk.api.config.Configuration;
import cz.proks.easyhelpdesk.api.exception.ApiException;
import cz.proks.easyhelpdesk.api.model.HelpdeskRole;
import cz.proks.easyhelpdesk.api.model.ThreadMember;

@Component(Configuration.Core.DB.THREAD_MEMBER_API)
public class ThreadMemberApiImpl extends BaseApiImpl<ThreadMember> implements ThreadMemberApi {
    private final Logger log = LoggerFactory.getLogger(getClass());
    
    @Autowired
    @Qualifier(Configuration.DB.THREAD_MEMBER_API)
    private ThreadMemberApi dbApi;

    @Override
    protected ThreadMemberApi getDbApi() {
        return dbApi;
    }
    
    @Override
    public ThreadMember getForHelpdeskMemberAndThread(Long helpdeskMemberId, Long threadId) throws ApiException {
        log.debug("getForHelpdeskMemberAndThread():: Start with helpdeskMemberId '{}', threadId '{}'", helpdeskMemberId, threadId);
        
        ThreadMember threadMember = getDbApi().getForHelpdeskMemberAndThread(helpdeskMemberId, threadId);
        
        log.debug("getForHelpdeskMemberAndThread():: End with '{}'", threadMember);
        return threadMember;
    }  
    
    @Override
    public List<ThreadMember> listForHelpdeskMember(Long helpdeskMemberId) throws ApiException {
        log.debug("listForHelpdeskMember():: Start with helpdeskMemberId '{}'", helpdeskMemberId);

        List<ThreadMember> threadMembers = getDbApi().listForHelpdeskMember(helpdeskMemberId);

        log.debug("listForHelpdeskMember():: End with {}", (threadMembers == null ? "null list" : "list of " + threadMembers.size() + " elements"));
        return threadMembers;
    }
    
    @Override
    public void setRead(Long id, boolean read) throws ApiException {
        log.debug("setRead():: Start with id '{}', read '{}'", id, read);
        
        getDbApi().setRead(id, read);
        
        log.debug("setRead():: Exit");
    }

    @Override
    public void shareForHelpdeskAndThreadAndRole(Long helpdeskId, Long threadId, HelpdeskRole helpdeskRole) throws ApiException {
        log.debug("shareForHelpdeskAndThreadAndRole():: Start with helpdeskId '{}', threadId '{}', helpdeskRole '{}'", new Object[] {helpdeskId, threadId, helpdeskRole});
        
        getDbApi().shareForHelpdeskAndThreadAndRole(helpdeskId, threadId, helpdeskRole);
        
        log.debug("shareForHelpdeskAndThreadAndRole():: Exit");        
    }

    @Override
    public void shareForHelpdeskMemberAndRole(Long helpdeskMemberId, HelpdeskRole helpdeskRole) throws ApiException {
        log.debug("shareForHelpdeskMemberAndRole():: Start with helpdeskMemberId '{}', helpdeskRole '{}'", helpdeskMemberId, helpdeskRole);
        
        getDbApi().shareForHelpdeskMemberAndRole(helpdeskMemberId, helpdeskRole);
        
        log.debug("shareForHelpdeskMemberAndRole():: Exit");
    }    
}
